package com.lzy.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lzy.health.dao.CheckItemDao;
import com.lzy.health.entity.PageResult;
import com.lzy.health.entity.QueryPageBean;
import com.lzy.health.entity.Result;
import com.lzy.health.exception.BusinessRuntimeException;
import com.lzy.health.pojo.CheckItem;
import com.lzy.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:
 * @Author
 * @Date 2021/8/30
 * @Version 1.0
 */
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Transactional
    public void add(CheckItem checkItem) {
        checkItemDao.insert(checkItem);
    }
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page=checkItemDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        log.info("[检查项-根据id删除]id：{}",id);
        //校验是否可以删除
        Long count=checkItemDao.countCheckGroupByCheckItemId(id);
        if(count>0){
            throw new BusinessRuntimeException("当前检查项有数据，不能删除");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        log.info("[检查项-根据ID查询]id:{}",id);
        CheckItem checkItem=checkItemDao.findById(id);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        log.info("[检查项-编辑]data:{}",checkItem);
        checkItemDao.edit(checkItem);
    }
}
